//
//  PPImageCompressHelper.m
//  pickpic
//
//  Created by GaoYongyue on 16/9/7.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPImageCompressHelper.h"
#import "UIImage+Resize.h"
@import ImageIO;
@import MobileCoreServices;
@implementation PPImageCompressHelper

+ (UIImage *)resizedImageWithImage:(UIImage *)image
{
    //写inSize大小的图
    CGSize size = image.size;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    screenSize.width *= [[UIScreen mainScreen] scale];
    screenSize.height *= [[UIScreen mainScreen] scale];

    CGSize destSzie = size;
    CGFloat ratio = size.width/size.height;

    //先生成一个Screen大小的图
    if (size.width > size.height)
    {
        //按照宽来缩图
        if (size.width < screenSize.width)
        {
            //比屏幕宽还小的话就不需要缩图了
            destSzie.width = size.width;
            destSzie.height = size.height;
        }
        else
        {
            //把宽缩到和屏幕宽度一样
            destSzie.width = screenSize.width;
            destSzie.height = destSzie.width / ratio;
        }
    }
    else
    {
        //按照高来缩图
        if (size.height < screenSize.height)
        {
            //比屏幕高还小的话就不需要缩图了
            destSzie.width = size.width;
            destSzie.height = size.height;
        }
        else
        {
            //把高缩到和屏幕高度一样
            destSzie.height = screenSize.height;
            destSzie.width = destSzie.height * ratio;
        }
    }

    //写屏幕大小的图
    UIImage *resizedImage = [image resizedImage:destSzie interpolationQuality:kCGInterpolationMedium];
    return resizedImage;
}

+ (UIImage *)resizedImageWithImage:(UIImage *)image
                        withinSize:(CGSize)inSize
{
    //写inSize大小的图
    CGSize size = image.size;

    CGSize destSzie = size;
    CGFloat ratio = size.width/size.height;

    //先生成一个inSize大小的图
    if (size.width > size.height)
    {
        //按照宽来缩图
        if (size.width < inSize.width)
        {
            //比inSize宽还小的话就不需要缩图了
            destSzie.width = size.width;
            destSzie.height = size.height;
        }
        else
        {
            //把宽缩到和inSize宽度一样
            destSzie.width = inSize.width;
            destSzie.height = destSzie.width / ratio;
        }
    }
    else
    {
        //按照高来缩图
        if (size.height < inSize.height)
        {
            //比inSize高还小的话就不需要缩图了
            destSzie.width = size.width;
            destSzie.height = size.height;
        }
        else
        {
            //把高缩到和inSize高度一样
            destSzie.height = inSize.height;
            destSzie.width = destSzie.height * ratio;
        }
    }

    UIImage *resizedImage = [image resizedImage:destSzie interpolationQuality:kCGInterpolationMedium];
    return resizedImage;
}

+ (NSData *)compressedDataWithImage:(UIImage *)image
{
    //写inSize大小的图
    NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
    if (imageData.length < 500*1024) //小于500kB不压缩
    {
        return imageData;
    }

    UIImage *resizedImage = [PPImageCompressHelper resizedImageWithImage:image];
    NSData *compressData = [[self class] compressDataWithImage:resizedImage];
    if (compressData == nil)
    {
        compressData = UIImageJPEGRepresentation(resizedImage, .8f);
    }
    return compressData;
}

+ (NSData *)compressedDataWithImage:(UIImage *)image
                         withinSize:(CGSize)inSize
{
    //写inSize大小的图
    NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
    if (imageData.length < 500*1024) //小于500kB不压缩
    {
        return imageData;
    }

    UIImage *resizedImage = [PPImageCompressHelper resizedImageWithImage:image withinSize:inSize];
    NSData *compressData = [[self class] compressDataWithImage:resizedImage];
    if (compressData == nil)
    {
        compressData = UIImageJPEGRepresentation(resizedImage, .8f);
    }
    return compressData;
}


+ (NSData *)compressDataWithImage:(UIImage *)image
{
    NSMutableData *processedData = [NSMutableData data];
    CGImageDestinationRef ref = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)processedData, kUTTypeJPEG, 1, NULL);

    CGImageDestinationSetProperties(ref, (__bridge CFDictionaryRef)@{(NSString *)kCGImageDestinationLossyCompressionQuality: @(.8)});
    CGImageDestinationAddImage(ref, image.CGImage, NULL);

    CGImageDestinationFinalize(ref);
    CFRelease(ref);
    return processedData;
}

@end
